<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="Spring使用配置类注入数据源连接mysql(未持久化)使用配置类可以代替xml文件的配置，从而摆脱枯燥的bean.xml pom.xml123456789101112131415161718192021222324252627&amp;lt;dependencies&amp;gt;    &amp;lt;dependency&amp;gt;        &amp;lt;groupId&amp;gt;org.springframework&amp;">
<meta name="keywords" content="Java,笔记,Spring">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring使用配置类注入数据源连接mysql（未持久化）">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;10&#x2F;18&#x2F;Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89&#x2F;index.html">
<meta property="og:site_name" content="ECIN&#39;S BLOG">
<meta property="og:description" content="Spring使用配置类注入数据源连接mysql(未持久化)使用配置类可以代替xml文件的配置，从而摆脱枯燥的bean.xml pom.xml123456789101112131415161718192021222324252627&amp;lt;dependencies&amp;gt;    &amp;lt;dependency&amp;gt;        &amp;lt;groupId&amp;gt;org.springframework&amp;">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-11-02T06:55:04.000Z">
<meta name="twitter:card" content="summary">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>Spring使用配置类注入数据源连接mysql（未持久化）</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/10/20/CPU%20GPU%20%E5%86%85%E5%AD%98%20%E7%BC%93%E5%AD%98%20%E7%A1%AC%E7%9B%98/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/10/18/Spring-%E6%B3%A8%E5%85%A5/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&text=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&is_video=false&description=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Spring使用配置类注入数据源连接mysql（未持久化）&body=Check out this article: http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/" target="_blank" rel="noopener"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&name=Spring使用配置类注入数据源连接mysql（未持久化）&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&t=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring使用配置类注入数据源连接mysql-未持久化"><span class="toc-number">1.</span> <span class="toc-text">Spring使用配置类注入数据源连接mysql(未持久化)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pom-xml"><span class="toc-number">1.0.1.</span> <span class="toc-text">pom.xml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#AppConfig类"><span class="toc-number">1.0.2.</span> <span class="toc-text">AppConfig类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DataBase类整合"><span class="toc-number">1.0.3.</span> <span class="toc-text">DataBase类整合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#主函数"><span class="toc-number">1.0.4.</span> <span class="toc-text">主函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#将ApplicationContext设为静态属性，可全局操作容器"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">将ApplicationContext设为静态属性，可全局操作容器</span></a></li></ol></li></ol></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Spring使用配置类注入数据源连接mysql（未持久化）
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">ECIN'S BLOG</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-10-18T12:30:45.000Z" itemprop="datePublished">2019-10-18</time>
        
      
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/Java/" rel="tag">Java</a>, <a class="tag-link" href="/tags/Spring/" rel="tag">Spring</a>, <a class="tag-link" href="/tags/%E7%AC%94%E8%AE%B0/" rel="tag">笔记</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="Spring使用配置类注入数据源连接mysql-未持久化"><a href="#Spring使用配置类注入数据源连接mysql-未持久化" class="headerlink" title="Spring使用配置类注入数据源连接mysql(未持久化)"></a>Spring使用配置类注入数据源连接mysql(未持久化)</h1><p>使用配置类可以代替xml文件的配置，从而摆脱枯燥的bean.xml</p>
<h3 id="pom-xml"><a href="#pom-xml" class="headerlink" title="pom.xml"></a>pom.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.1.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-beans<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.1.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.1.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-jdbc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.1.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>8.0.16<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="AppConfig类"><a href="#AppConfig类" class="headerlink" title="AppConfig类"></a>AppConfig类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.jdbc.datasource.DriverManagerDataSource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> DriverManagerDataSource <span class="title">dataSource</span><span class="params">()</span></span>&#123;</span><br><span class="line">        DriverManagerDataSource dataSource = <span class="keyword">new</span> DriverManagerDataSource();</span><br><span class="line">        dataSource.setDriverClassName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line">        dataSource.setUrl(<span class="string">"jdbc:mysql://localhost:3306/blog?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=UTC"</span>);</span><br><span class="line">        dataSource.setUsername(<span class="string">"root"</span>);</span><br><span class="line">        dataSource.setPassword(<span class="string">"123456"</span>);</span><br><span class="line">        <span class="keyword">return</span> dataSource;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> DataBase <span class="title">dataBase</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">        DataBase dataBase = <span class="keyword">new</span> DataBase(dataSource());</span><br><span class="line">        <span class="keyword">return</span> dataBase;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="DataBase类整合"><a href="#DataBase类整合" class="headerlink" title="DataBase类整合"></a>DataBase类整合</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.jdbc.datasource.DriverManagerDataSource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataBase</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    DriverManagerDataSource dataSource;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DataBase</span><span class="params">(DriverManagerDataSource dataSource)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.dataSource = dataSource;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">select</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">        Connection connect = dataSource.getConnection();</span><br><span class="line">        Statement statement = connect.createStatement();</span><br><span class="line">        ResultSet resultSet = statement.executeQuery(<span class="string">"SELECT * FROM type"</span>);</span><br><span class="line">        <span class="keyword">while</span>(resultSet.next())&#123;</span><br><span class="line">            System.out.println(resultSet.getString(<span class="number">1</span>)+<span class="string">" "</span>+resultSet.getString(<span class="number">2</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="主函数"><a href="#主函数" class="headerlink" title="主函数"></a>主函数</h3><h4 id="将ApplicationContext设为静态属性，可全局操作容器"><a href="#将ApplicationContext设为静态属性，可全局操作容器" class="headerlink" title="将ApplicationContext设为静态属性，可全局操作容器"></a>将ApplicationContext设为静态属性，可全局操作容器</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> config.AppConfig;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.ApplicationContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.AnnotationConfigApplicationContext;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> ApplicationContext context;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line"></span><br><span class="line">        context = <span class="keyword">new</span> AnnotationConfigApplicationContext(AppConfig.class);</span><br><span class="line">        DataBase dataBase = context.getBean(DataBase.class);</span><br><span class="line">        dataBase.select();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring使用配置类注入数据源连接mysql-未持久化"><span class="toc-number">1.</span> <span class="toc-text">Spring使用配置类注入数据源连接mysql(未持久化)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pom-xml"><span class="toc-number">1.0.1.</span> <span class="toc-text">pom.xml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#AppConfig类"><span class="toc-number">1.0.2.</span> <span class="toc-text">AppConfig类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DataBase类整合"><span class="toc-number">1.0.3.</span> <span class="toc-text">DataBase类整合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#主函数"><span class="toc-number">1.0.4.</span> <span class="toc-text">主函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#将ApplicationContext设为静态属性，可全局操作容器"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">将ApplicationContext设为静态属性，可全局操作容器</span></a></li></ol></li></ol></li></ol></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&text=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&is_video=false&description=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Spring使用配置类注入数据源连接mysql（未持久化）&body=Check out this article: http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/" target="_blank" rel="noopener"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&title=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&name=Spring使用配置类注入数据源连接mysql（未持久化）&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/10/18/Spring%E4%BD%BF%E7%94%A8%E9%85%8D%E7%BD%AE%E7%B1%BB%E6%B3%A8%E5%85%A5%E6%95%B0%E6%8D%AE%E6%BA%90%E8%BF%9E%E6%8E%A5mysql%EF%BC%88%E6%9C%AA%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%89/&t=Spring使用配置类注入数据源连接mysql（未持久化）" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 ECIN520
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">

    <!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<!-- clipboard -->

  <script src="/lib/clipboard/clipboard.min.js"></script>
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
